package com.xinzhi.controller;

import ch.qos.logback.core.util.FileUtil;
import com.alibaba.fastjson.JSON;
import com.xinzhi.entity.User;
import org.apache.commons.io.FileUtils;
import org.apache.coyote.Response;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.UUID;

@ResponseBody
@CrossOrigin
@Controller
public class UserController {

    @RequestMapping("/index1")
    public String show1(HttpServletRequest request, HttpServletResponse response, HttpSession session){
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        System.out.println(username);
        System.out.println(password);
        return "index.html";
    }

    @RequestMapping("/index2")
    public String show2(String username, String password){
        System.out.println(username);
        System.out.println(password);
    	return "index.html";
    }

    @RequestMapping("/index3")
    public String show3(@RequestParam("username") String username, @RequestParam("password") String password){
    	System.out.println(username);
    	System.out.println(password);
    	return "index.html";
    }

    @RequestMapping("/index4")
    public String show4(User u){
        System.out.println("----index4");
    	System.out.println(u.getUsername());
    	System.out.println(u.getPassword());
    	return "index.html";
    }

    @RequestMapping("/index5")
    public String show5(User u, String hobby){
    	System.out.println("----index5");
    	System.out.println(u.getUsername());
    	System.out.println(u.getPassword());
        System.out.println(hobby);
    	return "index.html";
    }

    @RequestMapping("/index6")
    public String show6(User u, String[] hobby){
        System.out.println("----index6");
        System.out.println(Arrays.toString(hobby));
    	return "index.html";
    }

    @RequestMapping("/index7")
    public String show7(@RequestParam("hobby") ArrayList<String> hobby){
        System.out.println("----index7");
        System.out.println(hobby.toString());
        return "index.html";
    }

    @RequestMapping("/index8")
    public String show8(@RequestBody ArrayList<User> users){
        System.out.println("----index8");
        System.out.println(users.toString());
        return "index.html";
    }

    @RequestMapping("/index9")
    public String show9(User u){
        System.out.println("----index9");
        System.out.println(u.toString());
        return "forward:index.html";
    }

    @RequestMapping("/index10")
    public String show10(User u){
        System.out.println("----index10");
        System.out.println(u.toString());
        return "redirect:index.html";
    }

    @RequestMapping("/index11")
    public ModelAndView show11(User u){
        System.out.println("----index11");
        ModelAndView mv = new ModelAndView();
        mv.addObject("user", u);
        mv.setViewName("index.html");
        return mv;
    }

    @RequestMapping("/index12")
    public String show12(User u){
        User user = JSON.parseObject("{\"username\": \"张帅扬\" , \"password\": \"123.com\"}", User.class);
        System.out.println(user.getUsername());
        System.out.println(user.getPassword());
        return JSON.toJSONString(user);
    }


    @RequestMapping("/fileupload")
    public String show13(MultipartFile uploadfile){
        String filename = uploadfile.getOriginalFilename();
        filename = UUID.randomUUID() + filename;
        String dirPath = "D:/file/";
        File filePath = new File(dirPath);
        if (!filePath.exists()){
            filePath.mkdir();
            System.out.println("123456");
        }
        try {
            uploadfile.transferTo(new File(dirPath + filename));
        } catch (IOException e) {
            e.printStackTrace();
            return "失败";
        }
        return "成功";
    }

    @RequestMapping("/download")
    public ResponseEntity<byte[]> fileDownload(String filename){
        String dirPath = "D:/file/";
        File file = new File(dirPath + filename);
        HttpHeaders headers = new HttpHeaders();
        headers.setContentDispositionFormData("attachment", filename);
        headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
        try {
            return new ResponseEntity<>(FileUtils.readFileToByteArray(file), headers, HttpStatus.OK);
        } catch (IOException e) {
            e.printStackTrace();
            return new ResponseEntity<>(e.getMessage().getBytes(), HttpStatus.EXPECTATION_FAILED);
        }
    }
}
